package com.brotherly.util;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Color;
import android.graphics.Paint;

public class TileUtil
{
    public static final Bitmap createRGBImage(Bitmap bitmap, int color)
    {
        int bitmap_w = bitmap.getWidth();
        int bitmap_h = bitmap.getHeight();
        int[] arrayColor = new int[bitmap_w * bitmap_h];
        int count = 0;
        for (int i = 0; i < bitmap_h; i++)
        {
            for (int j = 0; j < bitmap_w; j++)
            {
                int color1 = bitmap.getPixel(j, i);
                color1 = Color.argb(Color.alpha(color1), Color.red(color), Color.green(color), Color.blue(color));
                arrayColor[count] = color1;
                count++;
            }
        }
        bitmap = Bitmap.createBitmap(arrayColor, bitmap_w, bitmap_h, Config.ARGB_4444);
        return bitmap;
    }

    public static List<String> getTexts(String str, int width, Paint paint)
    {
        List<String> result = new ArrayList<String>(2);
        String first = getText(str, 0, str.length(), width, false, paint);
        result.add(first);
        if (first.length() < str.length())
        {
            result.add(getText(str, first.length(), str.length(), width, true, paint));
        }

        return result;
    }

    public static String getText(String str, int start, int end, int width, boolean islast, Paint paint)
    {
        float totalLength = paint.measureText(str, start, end);
        paint.breakText(str, start, end, true, width, null);
        if (totalLength > width)
        {
            if (islast)
            {
                float newWidth = width - paint.measureText("...   ");
                return str.substring(start, start + paint.breakText(str, start, end, true, newWidth, null)) + "...";
            } else
            {
                return str.substring(start, start + paint.breakText(str, start, end, true, width, null));
            }
        } else
        {
            return str.substring(start, end);
        }
    }

    public static List<String> get7Texts(String str, int width, Paint paint)
    {
        List<String> result = new ArrayList<String>(7);
        int start = 0;
        String first = null;
        while (start < str.length() && result.size() < 5)
        {
            if (result.size() > 3)
            {
                first = getText(str, start, str.length(), width, true, paint);
            } else
            {
                first = getText(str, start, str.length(), width, false, paint);
            }
            result.add(first);
            start += first.length();
        }
        return result;
    }
}
